package com.ljye.jg.ljye.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.TimePickerView;
import com.google.gson.Gson;
import com.ljye.jg.ljye.App;
import com.ljye.jg.ljye.R;
import com.ljye.jg.ljye.bean.Graduate_school;
import com.ljye.jg.ljye.utils.HttpUtils;
import com.ljye.jg.ljye.utils.Utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

//毕业院校
public class Activity_Graduate_School extends Activity {
    private String token;
    private OkHttpClient client;
    private ImageView back;//返回
    private TextView Consultation;//保存
    private EditText school;//学校
    private TextView starttime;//开始时间
    private TextView endtime;//结束时间
    private TextView xueli;//学历
    private EditText zhuanye;//专业
    private TimePickerView time;
    public View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.back://返回
                    finish();
                    break;
                case R.id.Consultation://保存
                    up_date();
                    break;
                case R.id.starttime://开始时间
                    time = new TimePickerView(Activity_Graduate_School.this, TimePickerView.Type.YEAR_MONTH_DAY);
                    time.setTime(new Date());
                    time.setCyclic(false);
                    time.setCancelable(true);
                    time.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
                        @Override
                        public void onTimeSelect(Date date) {
                            starttime.setText(getTime(date));
                        }
                    });
                    time.show();
                    break;
                case R.id.endtime://结束时间
                    time = new TimePickerView(Activity_Graduate_School.this, TimePickerView.Type.YEAR_MONTH_DAY);
                    time.setTime(new Date());
                    time.setCyclic(false);
                    time.setCancelable(true);
                    time.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
                        @Override
                        public void onTimeSelect(Date date) {
                            endtime.setText(getTime(date));
                        }
                    });
                    time.show();
                    break;
                case R.id.xueli://学历
                    OptionsPickerView options = new OptionsPickerView(Activity_Graduate_School.this);
                    final ArrayList<String> list = new ArrayList<>();
                    list.add("小学");
                    list.add("初中");
                    list.add("高中");
                    list.add("大专");
                    list.add("本科");
                    list.add("硕士");
                    list.add("博士");
                    options.setPicker(list);
                    options.setCyclic(false);
                    options.setSelectOptions(1);
                    options.setOnoptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
                        @Override
                        public void onOptionsSelect(int options1, int option2, int options3) {
                            xueli.setText(list.get(options1));
                        }
                    });
                    options.show();
                    break;
                default:
                    break;
            }
        }
    };
    private Gson gson;
    private Graduate_school.DataBean.SchoolBean schoolBean;

    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__graduate__school);
        initView();
        App application = (App) getApplication();
        token = application.getToken();
        client = App.getClient();
        getdata();
        back.setOnClickListener(listener);
        Consultation.setOnClickListener(listener);
        starttime.setOnClickListener(listener);
        endtime.setOnClickListener(listener);
        xueli.setOnClickListener(listener);

    }

    private void up_date() {
        final String schoolname = school.getText().toString().trim();
        final String start = starttime.getText().toString().trim();
        final String end = endtime.getText().toString().trim();
        final String xl = xueli.getText().toString().trim();
        final String zy = zhuanye.getText().toString().trim();

        new Thread() {
            @Override
            public void run() {
                RequestBody body = new MultipartBody.Builder().setType(MultipartBody.FORM)
                        .addFormDataPart("schoolname", schoolname != null ? schoolname : "输入学校名字")
                        .addFormDataPart("begin_time", start != null ? start : "请选择时间")
                        .addFormDataPart("end_time", end != null ? end : "请选择时间")
                        .addFormDataPart("education", xl != null ? xl : "请选择学历")
                        .addFormDataPart("major", zy != null ? zy : "输入专业名称,20字以内")
                        .build();

                Request request = new Request.Builder()
                        .url(HttpUtils.PATH + "/school")
                        .addHeader("Accept", HttpUtils.MYAPP)
                        .addHeader("Authorization", "Bearer" + " " + token)
                        .post(body)
                        .build();

                try {
                    Response execute = client.newCall(request).execute();
                    String up_string = execute.body().string();
                    Log.d("Activity_up", up_string);
                    if (execute.isSuccessful()) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(Activity_Graduate_School.this, "保存成功", Toast.LENGTH_SHORT).show();
                            }
                        });

                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }

    private void initView() {
        back = (ImageView) findViewById(R.id.back);
        Consultation = (TextView) findViewById(R.id.Consultation);
        school = (EditText) findViewById(R.id.school);
        starttime = (TextView) findViewById(R.id.starttime);
        endtime = (TextView) findViewById(R.id.endtime);
        xueli = (TextView) findViewById(R.id.xueli);
        zhuanye = (EditText) findViewById(R.id.zhuanye);
    }

    public void getdata() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Request request = new Request.Builder()
                            .url(HttpUtils.PATH + "/school")
                            .addHeader("Accept", HttpUtils.MYAPP)
                            .addHeader("Authorization", "Bearer" + " " + token)
                            .build();
                    Response response = client.newCall(request).execute();
                    String string = response.body().string();
                    Log.d("Activity_AA", string);
                    if (response.isSuccessful()) {
                        gson = new Gson();
                        Graduate_school school = gson.fromJson(string, Graduate_school.class);
                        schoolBean = school.getData().getSchool();
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                setdata();
                            }
                        });

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void setdata() {
        if (schoolBean != null) {
            if (schoolBean.getSchoolname() != null) {
                school.setHint(Utils.HintText(schoolBean.getSchoolname()));//学校
            }
            if (schoolBean.getBegin_time() != null) {
                starttime.setText(schoolBean.getBegin_time());//开始时间
            }
            if (schoolBean.getEnd_time() != null) {
                endtime.setText(schoolBean.getEnd_time());//结束时间
            }
            if (schoolBean.getEducation() != null) {
                xueli.setText(schoolBean.getEducation());//学历
            }
            if (schoolBean.getMajor() != null) {
                zhuanye.setHint(Utils.HintText(schoolBean.getMajor()));//专业
            }
        }

    }

    //收起软键盘
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            if (getCurrentFocus() != null && getCurrentFocus().getWindowToken() != null) {
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }
}